<?php

namespace App\Console\Commands\Tools;

use App\Kuafu\Pay\BasePay;
use App\Kuafu\Pay\Channel\FuYou\FuYouBase;
use App\Kuafu\Pay\Service\PayService;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\ShopCameraSyncService;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SyncShopCouncilorCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tools:refresh-shop-councilor';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "Refresh The Shop's Councilor";


    /**
     * Execute the console command
     *
     * @return int
     *
     */
    public function handle()
    {
        $this->info('全量更新门店督导脚本...开始');
        Config::set('database.connections.tiger', [
            'driver' => 'mysql',
            'url'    => '',


            'host'     => 'rm-bp1ii2tq3824rw7uzxo.mysql.rds.aliyuncs.com',
            'database' => 'tiger_prod',
            'username' => 'readonly',
            'password' => '4PuCQS5JXBYjc7V2eygf',

//            'host'     => 'rm-bp1066pu30bmyf7t19o.mysql.rds.aliyuncs.com',
//            'database' => 'kf_tiger',
//            'username' => 'kf_order',
//            'password' => 'order@1014',

            'port'           => '3306',
            'unix_socket'    => '',
            'charset'        => 'utf8mb4',
            'collation'      => 'utf8mb4_general_ci',
            'prefix'         => '',
            'prefix_indexes' => true,
            'strict'         => false,
            'engine'         => null,
            'options'        => extension_loaded('pdo_mysql') ? array_filter([
                \PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ]);

        $manageList = DB::connection('tiger')
                        ->table('open_shop_sign_order_permission_anchor')
                        ->leftJoin('open_shop_sign_order', 'open_shop_sign_order.orderNo', '=',
                            'open_shop_sign_order_permission_anchor.orderNo')
                        ->leftJoin('open_shop_order_manager', 'open_shop_sign_order.mainProcessId', '=',
                            'open_shop_order_manager.mainProcessId')
                        ->whereIn('open_shop_sign_order.status', [1, 2])
                        ->where('open_shop_sign_order.shopCode', '!=', 0)
//                        ->where('open_shop_sign_order.shopCode', 683)
                        ->where('open_shop_order_manager.type', 5)
                        ->where('open_shop_sign_order_permission_anchor.checkManagerId', '!=', 0)
                        ->get()
                        ->map(function ($value) {
                            return (array)$value;
                        })->toArray();


        $manageList = array_column($manageList, null, 'shopCode');

        $shopCodeList = array_keys($manageList);

        $shopList = Shop::query()->whereIn('shopCode', $shopCodeList)->orderBy('id')->get();

        foreach ($shopList as $one) {
            Shop::query()->where('id', $one->id)
                ->update(['councilorId' => $manageList[$one['shopCode']]['managerUid']]);
            $this->info('id:' . $one->id . '更新成功.');
        }

        $this->info('完成');
    }
}
